package com.open.party.cloud.viewModel;

import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import com.open.party.cloud.ServerConfig
import com.open.party.cloud.model.CommVo
import com.open.party.cloud.model.PartyTaskVo
import com.open.party.cloud.model.TaskBean
import com.open.party.cloud.model.data.Api
import com.open.party.cloud.model.task.TaskDetailEntity
import com.open.party.cloud.model.task.TaskRateEntity
import com.sinothk.rxretrofit.RxRetrofit
import org.greenrobot.eventbus.EventBus
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers


class TaskViewModel {

    // 我的》我的任务列表
    fun getTaskPageListForMeData(pageReq: PageReq<CommVo>) {

        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getTaskPageListForMeData(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<TaskBean>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<AppPageData<TaskBean>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = pageReq.data.seachflag
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<AppPageData<TaskBean>>) {
                    result.eventType = pageReq.data.seachflag
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun getTaskPageListForAllData(pageReq: PageReq<CommVo>) {

        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .getTaskPageListForAllData(pageReq)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<AppPageData<TaskBean>>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<AppPageData<TaskBean>>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = pageReq.data.seachflag
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<AppPageData<TaskBean>>) {
                    result.eventType = pageReq.data.seachflag
                    EventBus.getDefault().post(result)
                }
            })
    }

    fun loadTaskDetail(id: String) {

        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .loadTaskDetail(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<TaskDetailEntity>>() {

                override fun onCompleted() {}

                override fun onError(e: Throwable) {
                    val result = ResultInfo<TaskDetailEntity>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "loadTaskDetail"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(result: ResultInfo<TaskDetailEntity>) {
                    result.eventType = "loadTaskDetail"
                    EventBus.getDefault().post(result)
                }
            })
    }

    /**
     * 上传任务进度
     */
    fun uploadTaskRate(taskRateEntity: TaskRateEntity) {

        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .uploadTaskRate(taskRateEntity)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<TaskRateEntity>>() {

                override fun onCompleted() {
                }

                override fun onError(e: Throwable) {
                    val result = ResultInfo<TaskRateEntity>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "uploadTaskRate"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(resultData: ResultInfo<TaskRateEntity>) {
                    resultData.eventType = "uploadTaskRate"
                    EventBus.getDefault().post(resultData)
                }
            })
    }

    fun loadRateDetail(id: String) {

        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .loadRateDetail(id)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<TaskRateEntity>>() {

                override fun onCompleted() {
                }

                override fun onError(e: Throwable) {
                    val result = ResultInfo<TaskRateEntity>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "loadRateDetail"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(resultData: ResultInfo<TaskRateEntity>) {
                    resultData.eventType = "loadRateDetail"
                    EventBus.getDefault().post(resultData)
                }
            })
    }

    fun submitTaskCheck(vo: PartyTaskVo) {
        RxRetrofit.init(ServerConfig.SERVER_URL, ServerConfig.getHeader())
            .create(Api::class.java)
            .submitTaskCheck(vo)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Subscriber<ResultInfo<String>>() {

                override fun onCompleted() {
                }

                override fun onError(e: Throwable) {
                    val result = ResultInfo<String>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.message))
                    result.eventType = "submitTaskCheck"
                    EventBus.getDefault().post(result)
                }

                override fun onNext(resultData: ResultInfo<String>) {
                    resultData.eventType = "submitTaskCheck"
                    EventBus.getDefault().post(resultData)
                }
            })
    }
}
